package com.cloudkinto.base;

import com.cloudkinto.common.exception.BizException;
import com.cloudkinto.inteceptors.RequestHolder;
import com.cloudkinto.session.UserSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CrossOrigin;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zhangyy
 * @date 2020/4/16 14:16
 * @description:
 */
@CrossOrigin(origins = "*",maxAge = 3600)
public class BaseController {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    public UserSession getCurrentSession() {
        HttpServletRequest request = RequestHolder.getRequestLocal();
        if (null != request
                && null != request.getSession().getAttribute(UserSession.SESSION_ID_KEY)) {
            return UserSession.class.cast(request.getSession().getAttribute(UserSession.SESSION_ID_KEY));
        }
        return null;
    }

    public Long getCurrentUserId() {
        HttpServletRequest request = RequestHolder.getRequestLocal();
        if (null != request && null != request.getSession().getAttribute(UserSession.SESSION_ID_KEY)) {
            return UserSession.class.cast(request.getSession().getAttribute(UserSession.SESSION_ID_KEY)).getUserId();
        }
//        return 1L;
        throw new BizException("用户会话异常：查无用户信息userId");
    }
}
